package com.zxc.oj.ojcodesandbox.utils;

import cn.hutool.core.util.StrUtil;
import com.zxc.oj.ojcodesandbox.model.ExecuteMessage;
import org.springframework.util.StopWatch;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

public class ProcessUtils {

    /**
     * 获取程序执行信息
     *
     * @param process
     * @return
     */
    public static ExecuteMessage ProcessExecuteMessage(Process process, String opName) {
        ExecuteMessage executeMessage = new ExecuteMessage();
        try {
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
            int exitValue = process.waitFor();
            executeMessage.setExitValue(exitValue);
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            if (exitValue == 0) {
                System.out.println(opName + "成功");

                List<String> outputList = new ArrayList<>();
                String compileOutputLine;
                while ((compileOutputLine = reader.readLine()) != null) {
                    outputList.add(compileOutputLine);
                }
                executeMessage.setMessage(StrUtil.join("\n", outputList));
            } else {
                System.out.println(opName + "失败，错误码：" + exitValue);
                List<String> outputList = new ArrayList<>();
                String compileOutputLine;
                while ((compileOutputLine = reader.readLine()) != null) {
                    outputList.add(compileOutputLine);
                }
                executeMessage.setMessage(StrUtil.join("\n", outputList));
                //错误输出流
                BufferedReader readerError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
                List<String> errorOutputList = new ArrayList<>();
                String errorOutputLine;
                while ((errorOutputLine = readerError.readLine()) != null) {
                    errorOutputList.add(errorOutputLine);
                }
                executeMessage.setErrorMessage(StrUtil.join("\n", errorOutputList));
            }
            stopWatch.stop();
            executeMessage.setRunTime(stopWatch.getTotalTimeMillis());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            process.destroy();
        }
        return executeMessage;
    }


    /**
     * 获取程序执行信息
     *
     * @param process
     * @return
     */
    public static ExecuteMessage InteractProcessExecuteMessage(Process process, String args) {
        ExecuteMessage executeMessage = new ExecuteMessage();
        try (OutputStream outputStream = process.getOutputStream();
             OutputStreamWriter writer = new OutputStreamWriter(outputStream);
             BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        ) {
            String[] split = args.split(" ");
            String join = String.join("\n", split);
            writer.write(join + "\n");
            writer.flush();

            StringBuilder compileOutputStringBuilder = new StringBuilder();
            String compileOutputLine;
            while ((compileOutputLine = reader.readLine()) != null) {
                compileOutputStringBuilder.append(compileOutputLine);
            }
            executeMessage.setMessage(compileOutputStringBuilder.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return executeMessage;
    }


}
